<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCouponExternalTable extends Migration
{
    private $tableName = 'order_coupon_external';

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create($this->tableName, function (Blueprint $table) {
            $table->id();
            $table->integer('shopId')->default(0)->comment('店铺id');
            $table->unsignedBigInteger('orderId')->default(0)->comment('订单id');
            $table->unsignedTinyInteger('extPlatform')->default(0)->comment('外部平台 1美团');
            $table->string('couponSid')->default('')->comment('券id');
            $table->string('couponCode', 32)->default('')->comment('核销用券码');
            $table->string('couponTitle', 32)->default('')->comment('券名称');
            $table->unsignedTinyInteger('couponType')->default(0)->comment('券类型 1代金券 2套餐券');
            $table->unsignedInteger('userPay')->default(0)->comment('用户共支付的钱');
            $table->unsignedInteger('deduction')->default(0)->comment('券共可抵用金额');
            $table->tinyInteger('couponStatus')->default(0)->comment('核销状态 0默认 1已使用 2已撤销');

            $table->unsignedInteger('createdAt')->default(0)->comment('创建时间');
            $table->unsignedInteger('createdByUid')->default(0)->comment('创建人');
            $table->unsignedInteger('updatedAt')->default(0)->comment('修改时间');
            $table->unsignedInteger('updatedByUid')->default(0)->comment('修改人');

            $table->index('shopId', 'shopId_Idx');
            $table->index('orderId', 'orderId_Idx');
            $table->index('couponCode', 'couponCode_Idx');
            $table->index('createdAt', 'createdAt_Idx');

            $table->collation = 'utf8mb4_general_ci';
        });

        $prefix = config('other.DB_PREFIX', '');
        \Illuminate\Support\Facades\DB::statement("ALTER TABLE `{$prefix}$this->tableName` COMMENT = '订单核销三方平台券明细'"); //表注释
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists($this->tableName);
    }
}
